// Contact

function focusLabels() {
  var lables = document.getElementsByTagName("label");

  for (var i = 0; i < labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;
    labels[i].onclick = function() {
      var id = this.getAttribute("for");
      var element = document.getElementById(id);
      element.focus();
    }
  }
}


function resetFileds(form) {
  for (var i = 0; i < form.elements.length; i++) {
    var element = form.elements[i];
    if (element.type == "submit") continue;
    if (!element.getAttribute("placeholder")) continue;
    element.onfocus = function() {
      if (this.value == this.getAttribute("placeholder")) {
        this.value = "";
      }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.getAttribute("placeholder");
      }
    }
    element.onblur();
  }
}


function isFilled(field) {
  return (field.value.length > 0 && field.value != field.placeholder);
}


function isEmail(field) {
  return (field.value.indexOf("@") != -1  && field.value.indexOf(".") != -1);
}


function validateForm(form) {
  for (var i = 0; i < form.elements.length; i++) {
    var element = form.elements[i];
    if (element.getAttribute("required") == "required") {
      if (!isFilled(element)) {
        alert("Please fill in the " + element.name + " field.");
        return false;
      }
    }
    if (element.getAttribute("type") == "email") {
      if (!isEmail(element)) {
        alert ("The " + element.name + " field must be a valid email address.");
        return false;
      }
    }
  }

  return true;
}


function prepareForms() {
  for (var i = 0; i < document.forms.length; i++) {
    var form = document.forms[i];
    resetFileds(form);
    form.onsubmit = function() {
      if (!validateForm(this)) {
        return false;
      }
      var article = document.getElementsByTagName("article")[0];
      if (submitFormWithAjax(this, article)) {
        return false;
      }
      return true;
    }
  }
}


// Ajax


function displayAjaxLoading(element) {
  while (element.hasChildNodes()) {
    element.removeChild(element.lastChild);
  }

  var content = document.createElement("img");
  content.setAttribute("src", "images/loading.gif");
  content.setAttribute("alt", "Loading...");

  element.appendChild(content);
}


function submitFormWithAjax(form, target) {
  var request = new XMLHttpRequest();

  displayAjaxLoading(target);

  var dataParts = [];
  for (var i = 0; i < form.elements.length; i++) {
    var element = form.elements[i];
    dataParts[i] = element.name + "=" + encodeURIComponent(element.value);
  }

  var data = dataParts.join("&");

  request.open("POST", form.getAttribute("action"), true);
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      if (request.status == 200 || request.status == 0) {
        var matches = request.responseText.match(/<article>([\s\S]+)<\/article>/);
        if (matches.length > 0) {
          target.innerHTML = matches[1];
        } else {
          target.innerHTML = "<p>Oops, there was an error. Sorry.</p>";
        }
      } else {
        target.innerHTML = "<p>" + request.statusText + "</p>";
      }
    }
  };

  request.send(data);

  return true;
}


addLoadEvent(prepareForms);
